package com.ruoyi.riskrule.config.common;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.RuleIn;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.extdata.common.config.RuleConfig;
import com.ruoyi.riskrule.constants.Constants;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author cyh
 */
@Slf4j
public class RuleUtils {

    public static <T> T afterFormat(T clazz, String... excludeAttributes) {

        Set<String> excludeSet = new HashSet<>(Arrays.asList(new String[]{"serialVersionUID", "id","createTime","updateTime"}));
        if (excludeSet.size() > 0) {
            for (int i = 0; i < excludeAttributes.length; i++) {
                excludeSet.add(excludeAttributes[i]);
            }
        }

        Class<?> aClass = clazz.getClass();
        Field[] fields = aClass.getDeclaredFields();
        String methodName = "";
        for (int i = 0; i < fields.length; i++) {
            String attributeName = fields[i].getName();
            if (excludeSet.contains(attributeName)) {
                continue;
            }
            methodName = attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);
            try {
                boolean accessFlag = fields[i].isAccessible();
                fields[i].setAccessible(true);
                Object o = fields[i].get(clazz);
                if (Func.isEmpty(o)) {
                    Method getMethod = aClass.getMethod("set" + methodName, String.class);
                    getMethod.invoke(clazz, Constants.DEFAULT_VALUE);
                }
                fields[i].setAccessible(accessFlag);
            } catch (Exception e) {
                log.error("afterFormat 异常：", e);
            }
        }
        return clazz;
    }


    public static JSONObject riskParamConversion(RuleIn ruleIn) {
        //请求参数封装
        JSONObject riskParam = new JSONObject();
        riskParam.put("minDuedate", ruleIn.getMinDuedate());
        riskParam.put("restofamt", ruleIn.getRestofamt());
        riskParam.put("creditLine", ruleIn.getCreditLine());
        riskParam.put("entryId", ruleIn.getEntryId());
        riskParam.put("productCode", ruleIn.getProductCode());
        riskParam.put("runType", ruleIn.getRunType());
        riskParam.put("workflowCode", ruleIn.getWorkflowCode());

        riskParam.put("applyOverdueFlag", ruleIn.getApplyOverdueFlag());
        riskParam.put("inviterOverdueFlag", ruleIn.getInviterOverdueFlag());
        riskParam.put("deviceAllUserOverdueFlag", ruleIn.getDeviceAllUserOverdueFlag());
        riskParam.put("correlationUserOverdueFlag", ruleIn.getCorrelationUserOverdueFlag());

        riskParam.put("operType", ruleIn.getOperType());

        riskParam.put("custNo", ruleIn.getCustNo());

        riskParam.put("applyAge", ruleIn.getApplyAge());
        riskParam.put("userName", ruleIn.getUserName());
        riskParam.put("mobile", ruleIn.getMobile());
        riskParam.put("cardNo", ruleIn.getCardNo());
        riskParam.put("email", ruleIn.getEmail());
        riskParam.put("continent", ruleIn.getContinent());
        riskParam.put("loanUsed", ruleIn.getLoanUsed());
        riskParam.put("bvnNumber", ruleIn.getBvnNumber());

        riskParam.put("salary", ruleIn.getSalary());
        riskParam.put("jobtitle", ruleIn.getJobtitle());
        riskParam.put("industry", ruleIn.getIndustry());
        riskParam.put("marriage", ruleIn.getMarriage());
        riskParam.put("occupation", ruleIn.getOccupation());
        riskParam.put("pbrand", ruleIn.getPbrand());
        riskParam.put("ptype", ruleIn.getPtype());
        riskParam.put("opentime", ruleIn.getOpentime());
        riskParam.put("inchargetime", ruleIn.getInchargetime());
        riskParam.put("battery", ruleIn.getBattery());
        riskParam.put("tuoluoyi", ruleIn.getTuoluoyi());
        riskParam.put("maxstorage", ruleIn.getMaxstorage());
        riskParam.put("usedstorage", ruleIn.getUsedstorage());
        riskParam.put("operator", ruleIn.getOperator());
        riskParam.put("sim", ruleIn.getSim());
        riskParam.put("longitude", ruleIn.getLongitude());
        riskParam.put("latitude", ruleIn.getLatitude());
        riskParam.put("address", ruleIn.getAddress());
        riskParam.put("wifiname", ruleIn.getWifiname());
        riskParam.put("contactname", ruleIn.getContactname());
        riskParam.put("relation", ruleIn.getRelation());

        riskParam.put("education", ruleIn.getEducation());
        riskParam.put("contactMobile", ruleIn.getContactmobile());

        riskParam.put("deviceCountry", ruleIn.getDeviceCountry());
        riskParam.put("deviceCity", ruleIn.getDeviceCity());
        riskParam.put("deviceAppNum", ruleIn.getDeviceAppNum());
        riskParam.put("deviceIMEI", ruleIn.getDeviceimei());
        riskParam.put("deviceId", ruleIn.getDeviceId());
        riskParam.put("deviceRoot", ruleIn.getDeviceRoot());

        riskParam.put("cntcreditreject", ruleIn.getCntCreditReject());
        riskParam.put("cntcreditfail", ruleIn.getCntCreditFail());
        riskParam.put("cntbvn", ruleIn.getCntBvn());
        riskParam.put("cstmcntmail", ruleIn.getCstMcntMail());
        riskParam.put("cntcredit60days", ruleIn.getCntCredit60Days());
        riskParam.put("cntapply60days", ruleIn.getCntApply60Days());
        riskParam.put("cntwithdrawreject", ruleIn.getCntWithdrawReject());
        riskParam.put("cntimei", ruleIn.getCntImei());
        riskParam.put("cntdevice", ruleIn.getCntDevice());

        riskParam.put("apploannum", ruleIn.getAppLoanNum());
        riskParam.put("apploanguidenum", ruleIn.getAppLoanguideNum());
        riskParam.put("appbanknum", ruleIn.getAppBankNum());
        riskParam.put("apptransfernum", ruleIn.getAppTransferNum());

        riskParam.put("familycontactincontact", ruleIn.getFamilycontactInContact());
        riskParam.put("badcreditappnum", ruleIn.getBadCreditAppNum());
        riskParam.put("loantipguideappnum", ruleIn.getLoanTipGuideAppNum());
        riskParam.put("numcontactlikebvn", ruleIn.getNumContactLikeBvn());
        riskParam.put("numcontactlikebvnnumber", ruleIn.getNumContactLikeBvnNumber());
        riskParam.put("numcontactlikebvncust", ruleIn.getNumContactLikeBvnCust());
        riskParam.put("numcontactlikeloanapp", ruleIn.getNumContactLikeLoanapp());

        riskParam.put("orders", ruleIn.getOrders());
        riskParam.put("rejects", ruleIn.getRejects());
        riskParam.put("accepts", ruleIn.getAccepts());
        riskParam.put("recentresults", ruleIn.getRecentresults());

        riskParam.put("currentapplyline", ruleIn.getCurrentApplyLine());
        riskParam.put("currentapplyperiod", ruleIn.getCurrentApplyPeriod());
        riskParam.put("tradeorders", ruleIn.getTradeOrders());
        riskParam.put("traderejects", ruleIn.getTradeRejects());
        riskParam.put("tradeaccepts", ruleIn.getTradeAccepts());
        riskParam.put("traderesults", ruleIn.getTradeResults());

        riskParam.put("withdrawsucd", ruleIn.getWithdrawsucd());
        riskParam.put("zaidaihetong", ruleIn.getZaidaihetong());
        riskParam.put("zaidaibenjin", ruleIn.getZaidaibenjin());
        riskParam.put("jieqinghetong", ruleIn.getJieqinghetong());
        riskParam.put("jieqingbenjin", ruleIn.getJieqingbenjin());
        riskParam.put("maxyuqitianshu", ruleIn.getMaxyuqitianshu());
        riskParam.put("jieqinglixi", ruleIn.getJieqinglixi());
        riskParam.put("jieqingfuwufei", ruleIn.getJieqingfuwufei());
        riskParam.put("jieqingfaxi", ruleIn.getJieqingfaxi());
        riskParam.put("currentmaxyuqi", ruleIn.getCurrentmaxyuqi());

        riskParam.put("whdwsjcha1", ruleIn.getWhdwsjcha1());
        riskParam.put("applysjcha", ruleIn.getApplysjcha());
        riskParam.put("zdzhangdanricha", ruleIn.getZdzhangdanricha());

        riskParam.put("contactsno", ruleIn.getCntContactsNum());

        riskParam.put("overduenumincontacts", ruleIn.getOverdueNumInContacts());
        //androidno安卓版本号
        riskParam.put("androidno", ruleIn.getAndroidno());
        //APP版本
        riskParam.put("appversion", ruleIn.getAppVersion());

        riskParam.put("lasttimexds", ruleIn.getLasttimexds());

        riskParam.put("xdschecked", ruleIn.getXdschecked());

        riskParam.put("bindcardno", ruleIn.getBindcardno());
        riskParam.put("cntwifino", ruleIn.getCntwifino());
        riskParam.put("cntwifiodno", ruleIn.getCntwifiodno());

        riskParam.put("cntgpsno", ruleIn.getCntgpsno());
        riskParam.put("cntgpsodno", ruleIn.getCntgpsodno());

        //新加字段
        riskParam.put("lasttimereport", ruleIn.getLasttimereport());
        riskParam.put("reportchecked", ruleIn.getXdschecked());

        riskParam.put("entrytime", ruleIn.getEntrytime());
        riskParam.put("salarydate", ruleIn.getSalarydate());
        riskParam.put("workaddress", ruleIn.getWorkaddress());
        riskParam.put("workaddressdetail", ruleIn.getWorkaddressdetail());
        riskParam.put("residentialstatus", ruleIn.getResidentialstatus());
        riskParam.put("numofchildren", ruleIn.getNumofchildren());
        riskParam.put("religion", ruleIn.getReligion());
        riskParam.put("fristlanguage", ruleIn.getFristlanguage());
        riskParam.put("emcontactname", ruleIn.getEmcontactname());
        riskParam.put("emcontactnum", ruleIn.getEmcontactnum());
        riskParam.put("emrelation", ruleIn.getEmrelation());
        riskParam.put("jobtitleother", ruleIn.getJobtitleother());
        riskParam.put("occupationother", ruleIn.getOccupationother());
        riskParam.put("gender", ruleIn.getGender());
        riskParam.put("cntdevicetotal", ruleIn.getCntDeviceTotal());
        riskParam.put("cntdeviceapply", ruleIn.getCntDeviceApply());

        riskParam.put("cntdevicenew", ruleIn.getCntdevicenew());
        riskParam.put("fristcountrystate", ruleIn.getFristcountrystate());
        riskParam.put("contacthaschina", ruleIn.getContacthaschina());
        riskParam.put("allcontacthaschina", ruleIn.getAllcontacthaschina());

        riskParam.put("newdevicealluseroverdue", ruleIn.getNewDeviceAllUserOverdue());
        riskParam.put("cntnewdeviceapply", ruleIn.getCntNewDeviceApply());
        riskParam.put("cntnewdevicetotal", ruleIn.getCntnewdevicetotal());
        riskParam.put("expirydate", ruleIn.getExpiryDate());

        riskParam.put("usedrate", ruleIn.getUsedrate());
        riskParam.put("settletype", ruleIn.getSettletype());
        riskParam.put("settleodnum", ruleIn.getSettleodnum());
        riskParam.put("settledate", ruleIn.getSettledate());
        riskParam.put("originalline", ruleIn.getOriginalline());
        riskParam.put("autorised", ruleIn.getAutorised());
        riskParam.put("modellevel", ruleIn.getModellevel());
        riskParam.put("creditused", ruleIn.getCreditused());
        riskParam.put("risedinaday", ruleIn.getRisedinaday());

        riskParam.put("channel", ruleIn.getChannel());
        riskParam.put("stageResult", ruleIn.getStageResult());
        riskParam.put("invitedbynet", ruleIn.getInvitedbynet());

        riskParam.put("currentinterest", ruleIn.getCurrentinterest());
        riskParam.put("currentservicefee", ruleIn.getCurrentservicefee());
        riskParam.put("currentlatefee", ruleIn.getCurrentlatefee());
        riskParam.put("withholdamt", ruleIn.getWithholdamt());
        riskParam.put("withholdnums", ruleIn.getWithholdnums());

        riskParam.put("cntcontact1", ruleIn.getCntcontact1());
        riskParam.put("cntcontact2", ruleIn.getCntcontact2());
        riskParam.put("custrating", ruleIn.getCustrating());

        riskParam.put("cardineffectnum", ruleIn.getCardineffectnum());
        riskParam.put("sumbindcredit", ruleIn.getSumbindcredit());
        riskParam.put("countbindcredit", ruleIn.getCountbindcredit());
        riskParam.put("withholdcardnum", ruleIn.getWithholdcardnum());
        riskParam.put("accountnamematch", ruleIn.getAccountnamematch());

        riskParam.put("applyHour", ruleIn.getApplyHour());
        riskParam.put("applySucessHour", ruleIn.getApplySucessHour());

        riskParam.put("kycapiname", ruleIn.getKycapiname());
        riskParam.put("regphoneoperator", ruleIn.getRegphoneoperator());
        riskParam.put("bvnphoneoperator", ruleIn.getBvnphoneoperator());
        riskParam.put("kycbirthmatch", ruleIn.getKycbirthmatch());
        riskParam.put("diffpayandbill", ruleIn.getDiffpayandbill());
        riskParam.put("creditmodellevel", ruleIn.getCreditmodellevel());
        riskParam.put("reporttype", ruleIn.getReporttype());

        riskParam.put("bvnrelausermaxcnt", ruleIn.getBvnrelausermaxcnt());
        riskParam.put("bvncnt", ruleIn.getBvncnt());
        riskParam.put("accountrelausermaxcnt", ruleIn.getAccountrelausermaxcnt());
        riskParam.put("accountcnt", ruleIn.getAccountcnt());
        riskParam.put("cardrelausermaxcnt", ruleIn.getCardrelausermaxcnt());
        riskParam.put("cardcnt", ruleIn.getCardcnt());

        riskParam.put("deferredcnt",ruleIn.getDeferredcnt());
        riskParam.put("deferredhetong",ruleIn.getDeferredhetong());
        riskParam.put("deferredtotalday",ruleIn.getDeferredtotalday());
        riskParam.put("newfingerprint",ruleIn.getNewfingerprint());
        riskParam.put("currentdeferredcnt",ruleIn.getCurrentdeferredcnt());
        riskParam.put("currentdeferreddays",ruleIn.getCurrentdeferreddays());

        riskParam.put("whdwsjcha2",ruleIn.getWhdwsjcha2());
        return riskParam;
    }


    public static String getRuleAppValue(String value) {
        if (StringUtils.isNotBlank(value)) {
            return StringUtils.removeNonBmpUnicodes(value);
        }
        return "-1";
    }

}
